package webservices;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;

import databaseConnection.DBConnect;
import entity.Comment;
import entity.PostQuestion;

@WebService
public class HealthCareServer {
	User user;
	PostQuestion ques;

	private static DBConnect dbConnection = new DBConnect();

	public User login(String userName, String password) {
		Statement statement = dbConnection.getStatement();
		User user = null;
		boolean status = false;
		if (statement != null) {
			ResultSet result;
			try {
				password = convertToMD5(password);
				result = statement
						.executeQuery("Select * from aimsfirstconsultdb.users");
				while (result.next()) {
					if (result.getString("email").equalsIgnoreCase(userName)
							&& result.getString("password").equals(password)) {
						String userid = result.getString("user_id");
						String username = result.getString("first_name");
						String email = result.getString("email");
						// String userType = result.getString("usertype");
						user = new User();
						user.setUserId(userid);
						user.setEmail(email);
						user.setFirstName(username);
						// user.setUserType(userType);
						status = true;
						break;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");

		return user;
	}

	public Doctor doclogin(String userName, String password) {
		Statement statement = dbConnection.getStatement();
		Doctor doctor = null;
		boolean status = false;
		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("Select doctor_id,first_name,last_name,email,password from aimsfirstconsultdb.doctors");
				while (result.next()) {
					if (result.getString("email").equalsIgnoreCase(userName)
							&& result.getString("password").equals(password)) {
						String docid = result.getString("doctor_id");
						String docfirstname = result.getString("first_name");
						String email = result.getString("email");
						// String userType = result.getString("usertype");
						String doclastname = result.getString("last_name");

						doctor = new Doctor();
						doctor.setDocid(docid);
						doctor.setFirstname(docfirstname);
						doctor.setLastname(doclastname);
						doctor.setEmail(email);

						status = true;
						break;
					}

				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");

		return doctor;
	}

	public String registration(String fname, String lname, String email,
			String age, String sex, String password) {

		String firstname = fname;
		String lastname = lname;
		String useremail = email;
		String userage = age;
		String sx = sex;
		String userpassword = password;
		// boolean status = false;
		// user = new User();
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				statement
						.executeUpdate("Insert into aimsfirstconsultdb.users(first_name, last_name,email,age,gender,password) values('"
								+ firstname
								+ "','"
								+ lastname
								+ "','"
								+ useremail
								+ "','"
								+ userage
								+ "','"
								+ sx
								+ "','" + convertToMD5(userpassword) + "')");
				System.out.println("Inserted successfully!!");
				// status = true;
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return firstname;
	}

	private String convertToMD5(String input) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(input.getBytes());
		byte byteData[] = md.digest();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++)
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		return sb.toString();
	}

	/*
	 * public boolean postQues( String label, String ques, String useremail,
	 * String date){
	 * 
	 * String qlabel = label; String question = ques; String usremail =
	 * useremail; String qdate = date; //String userage = age; //String
	 * userpassword = password; boolean status = false; // user = new
	 * User(firstname, lastname, useremail, userage, password); Statement
	 * statement = dbConnection.getStatement(); if( statement != null ){ try {
	 * statement.executeUpdate(
	 * "Insert into aimsfirstconsultdb.question1(label,question,userEmail,createdDate) values('"
	 * +qlabel+"','"+question+"','"+usremail+"', '"+date+"')");
	 * System.out.println("Inserted successfully!!"); status = true; } catch
	 * (SQLException e) { e.printStackTrace(); } } else
	 * System.out.println("No Database connection!!!"); return status; }
	 */
	public String postQues(String label, String ques, String useremail,
			String date) {
		/*
		 * String qlabel = label; String question = ques; String usremail =
		 * useremail; String qdate = date;
		 */
		// String userage = age;
		// String userpassword = password;
		// boolean status = false;
		String[] datenew = date.split("-");
		assert datenew.length == 2;
		String status = "";
		String date1 = datenew[0];
		// String id1= columns[1];
		// user = new User(firstname, lastname, useremail, userage, password);
		Statement statement = dbConnection.getStatement();
		// ResultSet result;

		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("Select label from aimsfirstconsultdb.posts where userEmail='"
								+ useremail + "' and label ='" + label + "'");
				System.out.println("inside the executequery");
				if (result.next()) {
					// if(label.equals(result.getString(1)))
					status = "You have a post thread by this name! "
							+ label
							+ ". Would you like to comment on the existing thread or create a new thread by other label name";
					System.out.println("inside the result.next");
				} else {
					statement
							.executeUpdate("Insert into aimsfirstconsultdb.posts(label,question,userEmail,createdDate) values('"
									+ label
									+ "','"
									+ ques
									+ "','"
									+ useremail
									+ "', '" + date1 + "')");
					status = "Post is added!!";
					System.out.println("after the insertion");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("No Database connection!!!");
		}

		return status;
	}

	public String getPosts(String username) {
		String posts = "";
		String comments = "";
		Integer postId;
		String finalDelimiter = "";
		String delimiter = "";
		String result = "";

		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				ResultSet postsSet = statement
						.executeQuery("Select * from aimsfirstconsultdb.posts where userEmail='"
								+ username + "' order by createdDate desc");
				while (postsSet.next()) {
					posts += delimiter + postsSet.getString(1) + "%"
							+ postsSet.getString(2) + "%"
							+ postsSet.getString(3) + "%"
							+ postsSet.getString(4) + "%"
							+ postsSet.getString(5);
					delimiter = "$";
				}
				if (posts != "") {
					String[] postInfo = posts.split("\\$");

					for (int i = 0; i < postInfo.length; i++) {
						String[] postDetails = postInfo[i].split("%");
						postId = Integer.parseInt(postDetails[4]);
						delimiter = "";
						comments = "";
						ResultSet commentSet = statement
								.executeQuery("Select * from aimsfirstconsultdb.comments where idanswer='"
										+ postId
										+ "' order by lastmodifieddate desc");
						while (commentSet.next()) {
							comments += delimiter + commentSet.getString(1)
									+ "%" + commentSet.getString(2) + "%"
									+ commentSet.getString(3) + "%"
									+ commentSet.getString(4);
							delimiter = "$";
						}
						result += finalDelimiter + postInfo[i] + "&" + comments;
						finalDelimiter = "@";
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// System.out.println(result);
		return result;
	}

	public boolean deletePosts(int postId) {
		// String posts = "";
		// String delimiter = "";
		Statement statement = dbConnection.getStatement();
		boolean status = false;
		if (statement != null) {
			try {
				statement
						.executeUpdate("delete from aimsfirstconsultdb.posts where quesId='"
								+ postId + "'");

				statement
						.executeUpdate("delete from aimsfirstconsultdb.comments where idanswer='"
								+ postId + "'");
				status = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return status;
	}

	public void saveComment(String id, String comment, String user, String date) {
		// String status = "";
		int postid = Integer.parseInt(id);
		String[] datenew = date.split("-");
		assert datenew.length == 2;

		String date1 = datenew[0].trim();
		// allposts = posts[i].split("%");
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				statement
						.executeUpdate("Insert into aimsfirstconsultdb.comments values('"
								+ postid
								+ "','"
								+ comment
								+ "','"
								+ user
								+ "','" + date1 + "')");
				// status = ""
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// return ;
	}

	public String doclist(String docid, String userid) {
		String Status = "";
		int usid = Integer.parseInt(userid);
		int did = Integer.parseInt(docid);
		ResultSet result;
		// allposts = posts[i].split("%");
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {

				result = statement
						.executeQuery("Select docid from aimsfirstconsultdb.docusers where userid='"
								+ usid + "' and docid='" + did + "'");
				System.out.println("inside the executequery");
				if (result.next()) {
					// if(label.equals(result.getString(1)))
					Status = "The doctor is already in your list";
					System.out.println("inside the result.next");
				} else {
					statement
							.executeUpdate("Insert into aimsfirstconsultdb.docusers values('"
									+ usid + "','" + did + "')");
					Status = "Doctor added to your list";
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return Status;
	}

	/**
	 * 
	 * @param userid
	 * @param spec
	 * @return
	 */
	public String getDocList(String userid, String spec) {
		int usid = Integer.parseInt(userid);
		Statement statement = dbConnection.getStatement();
		String doclist = "";
		String delimiter = "";
		// List<Doctor> doclist = (List<Doctor>) new Doctor();
		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("SELECT doctors.doctor_id, doctors.first_name, doctors.last_name,doctors.email,doctors.gender,doctors.specialization ,doctors.clinic_name,doctors.clinic_address,doctors.clinic_zip,doctors.phone,doctors.hours,doctors.website,doctors.about_me,doctors.education,doctors.experience FROM aimsfirstconsultdb.doctors inner join aimsfirstconsultdb.docusers where doctors.doctor_id = docusers.docid and docusers.userid = '"
								+ usid
								+ "' and doctors.specialization='"
								+ spec + "'");
				while (result.next()) {

					doclist += delimiter + result.getString("first_name") + "%"
							+ result.getString("last_name") + "%"
							+ result.getString("phone") + "%"
							+ result.getString("Specialization") + "%"
							+ result.getString("doctor_id");
					delimiter = "$";
				}
				System.out.println(doclist);
				// return doclist;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return doclist;
		/*
		 * Doctor doctor = new Doctor();
		 * doctor.setFirstname(result.getString("first_name"));
		 * doctor.setLastname(result.getString("last_name"));
		 * doctor.setPhone(result.getString("phone"));
		 * doctor.setHours(result.getString("hours"));
		 * doctor.setSpecialization(result.getString("Specialization"));
		 * //doctor.setDoctorid(result.getString("Specialization"));
		 * doclist.add(doctor);
		 */

	}

	public String postQuesDoc(String doctorid, String label, String ques,
			String userid, String date) {
		/*
		 * String qlabel = label; String question = ques; String usremail =
		 * useremail; String qdate = date;
		 */
		// String userage = age;
		// String userpassword = password;
		// boolean status = false;
		String[] datenew = date.split("-");
		assert datenew.length == 2;
		String status = "";
		String date1 = datenew[0].trim();
		// System.out.println(date1);
		// String id1= columns[1];
		// user = new User(firstname, lastname, useremail, userage, password);
		Statement statement = dbConnection.getStatement();
		// ResultSet result;
		String docids[] = doctorid.split("\\,");

		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("Select label from aimsfirstconsultdb.docposts where userid='"
								+ userid + "' and label ='" + label + "'");
				System.out.println("inside the executequery");
				if (result.next()) {
					// if(label.equals(result.getString(1)))
					status = "You have a post thread by this name! "
							+ label
							+ ". Would you like to comment on the existing thread or create a new thread by other label name";
					System.out.println("inside the result.next");
				} else {
					for (int i = 0; i < docids.length; i++) {
						String docid = docids[i];
						statement
								.executeUpdate("Insert into aimsfirstconsultdb.docposts(label,question,userid,docid,createdDate) values('"
										+ label
										+ "','"
										+ ques
										+ "','"
										+ userid
										+ "','" + docid + "','" + date1 + "')");
						status = "Post is added!!";
						System.out.println("after the insertion");
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("No Database connection!!!");
		}

		return status;
	}

	public String getDocPosts(String userid, String docids) {
		String posts = "";
		String comments = "";
		Integer postId;
		String finalDelimiter = "";
		String delimiter = "";
		String result = "";
		String dids[] = docids.split("\\,");
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				for (int i = 0; i < dids.length; i++) {
					String docid = dids[i];
					System.out.println("docid in getDocPosts" + docid);
					ResultSet postsSet = statement
							.executeQuery("Select  concat(doctors.first_name, ' ', doctors.last_name) as docname, label,question,userid,docid,quesId,createdDate FROM aimsfirstconsultdb.doctors inner join aimsfirstconsultdb.docposts where doctors.doctor_id = docposts.docid  and userid='"
									+ userid
									+ "' and docid='"
									+ docid
									+ "' order by createdDate desc");
					while (postsSet.next()) {
						posts += delimiter + postsSet.getString(1) + "%"
								+ postsSet.getString(2) + "%"
								+ postsSet.getString(3) + "%"
								+ postsSet.getString(4) + "%"
								+ postsSet.getString(5) + "%"
								+ postsSet.getString(6) + "%"
								+ postsSet.getString(7);
						System.out.println(posts);
						delimiter = "$";
					}
				}
				if (posts != "") {
					String[] postInfo = posts.split("\\$");

					for (int i = 0; i < postInfo.length; i++) {
						String[] postDetails = postInfo[i].split("%");
						postId = Integer.parseInt(postDetails[5]);
						delimiter = "";
						comments = "";
						ResultSet commentSet = statement
								.executeQuery("Select * from aimsfirstconsultdb.doccomments where idanswer='"
										+ postId
										+ "' order by createddate desc");
						while (commentSet.next()) {
							comments += delimiter + commentSet.getString(1)
									+ "%" + commentSet.getString(2) + "%"
									+ commentSet.getString(3) + "%"
									+ commentSet.getString(4);
							delimiter = "$";
						}
						result += finalDelimiter + postInfo[i] + "&" + comments;
						finalDelimiter = "@";
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// System.out.println(result);
		return result;
	}

	public boolean deleteDocPosts(int postId) {
		// String posts = "";
		// String delimiter = "";
		Statement statement = dbConnection.getStatement();
		boolean status = false;
		if (statement != null) {
			try {
				statement
						.executeUpdate("delete from aimsfirstconsultdb.docposts where quesId='"
								+ postId + "'");
				System.out.println("inside the docdeletepost");
				statement
						.executeUpdate("delete from aimsfirstconsultdb.doccomments where idanswer='"
								+ postId + "'");

				System.out.println("after the docdeletepost");
				status = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return status;
	}

	public void savedocComment(String id, String comment, String user,
			String date) {
		// String status = "";
		int postid = Integer.parseInt(id);
		// allposts = posts[i].split("%");
		String[] datenew = date.split("-");
		assert datenew.length == 2;

		String date1 = datenew[0].trim();
		System.out.println(date1);
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				statement
						.executeUpdate("Insert into aimsfirstconsultdb.doccomments values('"
								+ postid
								+ "','"
								+ comment
								+ "','"
								+ user
								+ "','" + date1 + "')");

				System.out.println("after doccomment insertion");
				// status = ""
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// return ;
	}

	/* Method returns the users list of the doctors */

	public String getDocusersList(String docid) {
		int usid = Integer.parseInt(docid);
		Statement statement = dbConnection.getStatement();
		String userlist = "";
		String delimiter = "";
		// List<Doctor> doclist = (List<Doctor>) new Doctor();
		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("SELECT * FROM aimsfirstconsultdb.users inner join aimsfirstconsultdb.docusers where users.user_id = docusers.userid and docusers.docid = '"
								+ usid + "'");
				while (result.next()) {

					userlist += delimiter + result.getString("first_name")
							+ "%" + result.getString("last_name") + "%"
							+ result.getString("age") + "%"
							+ result.getString("email") + "%"
							+ result.getString("user_id");
					delimiter = "$";
				}
				System.out.println(userlist);
				// return doclist;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return userlist;

	}

	/* Doctor posting an article */

	public String postArticle(String label, String article, String doctorid,
			String tags, String date) {

		String[] datenew = date.split("-");
		assert datenew.length == 2;
		String status = "";
		String date1 = datenew[0];
		String specialization = "";
		String doctor = "";
		Statement statement = dbConnection.getStatement();
		int id = Integer.parseInt(doctorid);
		int docid = Integer.parseInt(doctorid);

		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("Select label from aimsfirstconsultdb.articles where docid='"
								+ docid + "' and label ='" + label + "'");
				System.out.println("inside the executequery");
				if (result.next()) {
					// if(label.equals(result.getString(1)))
					status = "You have a article posted by this name! "
							+ label
							+ " . Please  create a new article by other label name";
					System.out.println("inside the result.next");
				} else {
					result = statement
							.executeQuery("Select concat(doctors.first_name, ' ', doctors.last_name) as docname, specialization from aimsfirstconsultdb.doctors where doctor_id='"
									+ id + "'");
					System.out.println("inside the executequery");
					if (result.next()) {
						specialization = result.getString("specialization");
						doctor = result.getString("docname");
					}
					statement
							.executeUpdate("Insert into aimsfirstconsultdb.articles(label,article,docid,tags,createdDate, specialization,doctor) values('"
									+ label
									+ "','"
									+ article
									+ "','"
									+ docid
									+ "','"
									+ tags
									+ "','"
									+ date1
									+ "', '"
									+ specialization + "','" + doctor + "')");
					status = "Article is added!!";
					System.out.println("after the insertion");
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			System.out.println("No Database connection!!!");
		}

		return status;
	}

	public String getDocArticlesList(String docid) {
		int usid = Integer.parseInt(docid);
		Statement statement = dbConnection.getStatement();
		String articleList = "";
		String delimiter = "";
		// List<Doctor> doclist = (List<Doctor>) new Doctor();
		if (statement != null) {
			ResultSet result;
			try {
				result = statement
						.executeQuery("SELECT * FROM aimsfirstconsultdb.articles where docid = '"
								+ usid + "' order by createdDate desc");
				while (result.next()) {

					articleList += delimiter + result.getString("label") + "%"
							+ result.getString("article") + "%"
							+ result.getString("tags") + "%"
							+ result.getString("createdDate") + "%"
							+ result.getString("articleid");
					delimiter = "$";
				}
				System.out.println(articleList);
				// return doclist;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return articleList;

	}

	public boolean deleteUsers(int userid, int docid) {
		// String posts = "";
		// String delimiter = "";
		Statement statement = dbConnection.getStatement();
		boolean status = false;
		if (statement != null) {
			try {
				statement
						.executeUpdate("delete from aimsfirstconsultdb.docusers where docid='"
								+ docid + "' and userid = '" + userid + "'");

				status = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return status;
	}

	public boolean deleteDoctors(int userid, int docid) {
		// String posts = "";
		// String delimiter = "";
		// String usid = (String)userid;
		Statement statement = dbConnection.getStatement();
		boolean status = false;
		if (statement != null) {
			try {
				statement
						.executeUpdate("delete from aimsfirstconsultdb.docusers where docid='"
								+ docid + "' and userid = '" + userid + "'");
				// statement.executeUpdate("delete from aimsfirstconsultdb.docposts where docid='"+docid+"' and userid = '"+userid+"'");
				// statement.executeUpdate("delete from aimsfirstconsultdb.doccomments where docid='"+docid+"' and userid = '"+userid+"'");
				status = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return status;
	}

	public boolean deleteArticles(int docid, int articleid) {
		// String posts = "";
		// String delimiter = "";
		Statement statement = dbConnection.getStatement();
		boolean status = false;
		if (statement != null) {
			try {
				statement
						.executeUpdate("delete from aimsfirstconsultdb.articles where docid='"
								+ docid
								+ "' and articleid = '"
								+ articleid
								+ "'");

				status = true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		return status;
	}

	public String getDocUserPosts(String userid, String docid) {
		String posts = "";
		String comments = "";
		Integer postId;
		String finalDelimiter = "";
		String delimiter = "";
		String result = "";
		int did = Integer.parseInt(docid);
		// String dids[] = docids.split("\\,");
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				// String docid = dids[i];
				System.out.println("docid in getDocPosts" + docid);
				System.out.println("user in getDocPosts" + userid);
				ResultSet postsSet = statement
						.executeQuery("Select  label,question,userid,docid,quesId,createdDate FROM aimsfirstconsultdb.doctors inner join aimsfirstconsultdb.docposts where doctors.doctor_id = docposts.docid  and userid='"
								+ userid
								+ "' and docid='"
								+ docid
								+ "' order by createdDate desc");
				while (postsSet.next()) {
					posts += delimiter + postsSet.getString(1) + "%"
							+ postsSet.getString(2) + "%"
							+ postsSet.getString(3) + "%"
							+ postsSet.getString(4) + "%"
							+ postsSet.getString(5) + "%"
							+ postsSet.getString(6);
					System.out.println(posts);
					delimiter = "$";

				}
				if (posts != "") {
					String[] postInfo = posts.split("\\$");

					for (int i = 0; i < postInfo.length; i++) {
						String[] postDetails = postInfo[i].split("%");
						postId = Integer.parseInt(postDetails[4]);
						System.out.println("postid inside posts" + postId);
						delimiter = "";
						comments = "";
						ResultSet commentSet = statement
								.executeQuery("Select * from aimsfirstconsultdb.doccomments where idanswer='"
										+ postId
										+ "' order by createddate desc");
						while (commentSet.next()) {
							comments += delimiter + commentSet.getString(1)
									+ "%" + commentSet.getString(2) + "%"
									+ commentSet.getString(3) + "%"
									+ commentSet.getString(4);
							delimiter = "$";
						}
						result += finalDelimiter + postInfo[i] + "&" + comments;
						finalDelimiter = "@";
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// System.out.println(result);
		return result;
	}

	public void savedocUserComment(String id, String comment, String user,
			String date) {
		// String status = "";
		int postid = Integer.parseInt(id);
		// allposts = posts[i].split("%");
		String[] datenew = date.split("-");
		assert datenew.length == 2;

		String date1 = datenew[0].trim();
		System.out.println(date1);
		Statement statement = dbConnection.getStatement();
		if (statement != null) {
			try {
				statement
						.executeUpdate("Insert into aimsfirstconsultdb.doccomments values('"
								+ postid
								+ "','"
								+ comment
								+ "','"
								+ user
								+ "','" + date1 + "')");

				System.out.println("after doccomment insertion");
				// status = ""
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else
			System.out.println("No Database connection!!!");
		// return ;
	}

}
